﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Types;

namespace Storage
{
    using Exceptions;
    using EntityModels;

    #region StorageManager

    /// <summary>
    /// Rapresents the way in which the modules can handle and persist their own data.
    /// </summary>
    public sealed partial class StorageManager
    {
        #region Private methods

        #region tryRemoveEvent

        /// <summary>
        /// Check if there are some references for the Event with Id = evtID.
        /// If no, deletes the entry of Events with Id = evtID.
        /// </summary>
        /// <param name="evtID">The event's id.</param>
        private void tryRemoveEvent(int evtID)
        {
            StorageManager.Instance.Entities.Events_delete(evtID);
        }

        #endregion

        #endregion

        #region Public methods

        #region GetEventById

        /// <summary>
        /// Returns a event selected by eventID.
        /// </summary>
        /// <param name="eventID">The eventID</param>
        /// <returns>A event selected by id</returns>
        public Event GetEventById(int eventID)
        {
            try
            {
                return Entities.Events.First(e => e.Id == eventID);
            }
            catch (InvalidOperationException)
            {
                throw new EventNotFoundException(eventID);
            }
        }

        #endregion

        #region DeleteEventById

        /// <summary>
        /// Delete the event with the specified id.
        /// </summary>
        /// <param name="evtID">The event Id.</param>
        public void DeleteEventById(int evtID)
        {
            try
            {            
                tryRemoveEvent(evtID);
            }
            catch (InvalidOperationException)
            {
                throw new EventNotFoundException(evtID);
            }
        }

        #endregion

        #endregion
    } 
    #endregion
}